import time
import requests
import re
import threading

def sen_requests(url):
    """发送请求"""
    response = requests.get(url=url)
    return response  # 返回相应 对象

def pares_data(data):   # 返回一个列表
    """解析数据"""
    resilt_list = re.findall('<img class="ui image lazy" data-original="(.*?)"', data, re.S)
    return resilt_list


def save_data(file_name, data):
    """保存数据"""
    with open(f'img2\\{file_name}', mode='wb') as f:
        f.write(data)
        print(f'{file_name}下载成功！！！！')


def run(url):     # 多线程只能操作一个函数
    # 1，调用请求数据的函数
    html_data = sen_requests(url).text
    # 2, 调用数据解析的函数
    ing_url_list = pares_data(html_data)
    # print(ing_url_list)
    for img_url in ing_url_list:
        img_data = sen_requests(img_url).content
        file_name = img_url.split('/')[-1]
        # 调用保存数据的函数
        save_data(file_name, img_data)

    print('花费的时间是：', time.time() - start_tine)


if __name__ == '__main__':
    # url = 'https://www.fabiaoqing.com/biaoqing/lists/page/1.html'
    # run(url)
    # 多线程
    start_tine = time.time()
    for page in range(1, 11):
        url = f'https://www.fabiaoqing.com/biaoqing/lists/page/{page}.html'

        threading.Thread(target=run, args=(url, )).start()





